Conversation
| @DecorateEn | ||
| public exec(): string { | ||
| return (this.a + this.b).toString(); | ||
| } |
There was a problem hiding this comment.
Это не патерн декоратор
https://refactoring.guru/ru/design-patterns/decorator/typescript/example
| @confirmMail | ||
| public email: string = "misha@mail.ru"; | ||
| } | ||
|
|
|
|
||
| /**Класс описывает SelectBox контрол */ | ||
| class SelectBox extends Control<SelectItem> { | ||
| private item: SelectItem; |
There was a problem hiding this comment.
зачем? у тебя ешь шаблонное поле value, которое выполняет туже самую функцию
| } | ||
|
|
||
| public register<?>(type: ?) { | ||
| public register(type: Function) { |
There was a problem hiding this comment.
тут должны были использоваться generic-типы.
| @@ -61,5 +102,5 @@ factory.register(SelectBox); | |||
|
|
|||
| const selectBoxInstance = factory.getInstance(SelectBox); | |||
There was a problem hiding this comment.
т.к. не используются generic-типы в этот метод можно передать любой тип.
Задача решена не верна
|
|
||
| const x = undefined; | ||
| const x = { "year": 2016 }; | ||
|
|
| @validate(ValueExample2, "booleanProp") | ||
| public propValueExample2: any; | ||
| } | ||
| Object.defineProperty(target, propertyName, { |
There was a problem hiding this comment.
Это не обязательно, декоратор итак возвращает дескриптор
| return type; | ||
| }, | ||
| set(newType) { | ||
| if (newType === type) { |
| this.booleanProp = booleanProp; | ||
| } | ||
| } | ||
| function validate(type: object, propertyKey: string) { |
There was a problem hiding this comment.
нет проверки на принадлежность поля к типу. приложил правильное решени
|
|
||
| @validate(ValueExample2, "booleanProp") | ||
| public propValueExample2: any; | ||
| } No newline at end of file |
There was a problem hiding this comment.
function validate<T, P extends keyof T>(type: new () => T, prop: P): (target: Object, propertyKey: string | symbol) => any {
let temp: T;
return (target: Object, propertyKey: string | symbol): PropertyDescriptor => {
let descriptor: PropertyDescriptor = {
get: function () {
return temp;
},
set: function (val: T) {
if (!(val instanceof type)) {
throw new Error("Устанавливается значение которое не соотвтетсвует типу");
}
if (temp[prop]) {
throw new Error("У требуемого поля не верный примитивный тип");
}
console.log("value valid");
temp = val;
}
};
return descriptor;
};
}
class ValueExample1 {
public value: string;
public id: number;
}
class ValueExample2 {
public prop3: undefined;
public prop2: boolean;
}
class Example {
@Validate(ValueExample1, "id1")
public propValueExample1: any;
@validate(ValueExample2, "prop3")
public propValueExample2: any;
}
let ex1 = new Example();
let objValExample = new ValueExample1();
objValExample.id = 1;
objValExample.value = "qwe";
ex1.propValueExample1 = objValExample;
ex1.propValueExample2 = false;
|
итого 6 балов |
No description provided.